package com.gonetbar.base.aop;

import java.lang.reflect.Method;

import org.apache.log4j.Logger;
import org.springframework.aop.ThrowsAdvice;

import com.gonetbar.base.mail.SpeedAdminJavaMailSender;
import com.gonetbar.base.vo.SpeedAdminEmailMessage;

/**
 * 
 * @Description:
 * 
 * @author weisd Email:xiyangdewuse@163.com
 * @date 2011-4-17 上午11:00:58
 * @version v1.0
 */
public class DaoExceptionInterceptor implements ThrowsAdvice {

	private boolean listen = true;
	private SpeedAdminJavaMailSender sqlWaringJavaMailSender;
	private SpeedAdminEmailMessage sem;

	public SpeedAdminEmailMessage getSem() {
		return sem;
	}

	public void setSem(SpeedAdminEmailMessage sem) {
		this.sem = sem;
	}

	private static final Logger errorLog = Logger.getLogger("errorlogfile");

	public void afterThrowing(Method method, Object[] args, Object target,
			Exception ex) throws Throwable {
		
		System.out.println("---------------------------------------");
//		if (this.listen) {
//			ParamObjVObj paramObjVObj = (ParamObjVObj) args[0];
//			Map inParamsMap = null;
//			StringBuffer logInfo = new StringBuffer();
//			boolean isMap = this.isMap(paramObjVObj.getCondition());
//			if (isMap == true) {
//				inParamsMap = new HashMap((Map) paramObjVObj.getCondition());
//				logInfo.append(this.writeLog(paramObjVObj, inParamsMap,
//						inParamsMap));
//			} else {
//				logInfo.append("Method Name: " + method.getName()
//						+ " DataSource:");
//				logInfo.append(paramObjVObj.getDataSourceMapping());
//				logInfo.append(" Sqlid:");
//				logInfo.append(paramObjVObj.getSqlId());
//				logInfo.append(" SQL:");
//				logInfo.append(paramObjVObj.getSql());
//			}
			errorLog.error("-----------测试 日志异常---------", ex);
			// ex.printStackTrace();
//			this.sem.setText(errorLog.toString());
//			sqlWaringJavaMailSender.senderMail(this.sem);
//		}
	}

	public boolean isListen() {
		return listen;
	}

	public void setListen(boolean listen) {
		this.listen = listen;
	}

	public SpeedAdminJavaMailSender getSqlWaringJavaMailSender() {
		return sqlWaringJavaMailSender;
	}

	public void setSqlWaringJavaMailSender(
			SpeedAdminJavaMailSender sqlWaringJavaMailSender) {
		this.sqlWaringJavaMailSender = sqlWaringJavaMailSender;
	}

	// private String writeLog(ParamObjVObj paramObjVObj, Map inParams,
	// Map allParams) {
	// StringBuffer logInfo = new StringBuffer();
	// logInfo.append("DataSource:");
	// logInfo.append(paramObjVObj.getDataSourceMapping());
	// logInfo.append(" Sqlid:");
	// logInfo.append(paramObjVObj.getSqlId());
	// logInfo.append(" PROCEDURE:");
	// logInfo.append(paramObjVObj.getSql());
	// Map outParams = new HashMap(allParams);
	// Set tempSet = inParams.entrySet();
	// Iterator it = tempSet.iterator();
	// Entry e = null;
	// logInfo.append(" IN PARAMS: ");
	// while (it.hasNext()) {
	// e = (Entry) it.next();
	// logInfo.append(e.getKey());
	// logInfo.append("=");
	// logInfo.append(e.getValue());
	// logInfo.append(",");
	// outParams.remove(e.getKey());
	// }
	// logInfo.append(" OUT PARAMS: ");
	// tempSet = outParams.entrySet();
	// it = tempSet.iterator();
	// while (it.hasNext()) {
	// e = (Entry) it.next();
	// logInfo.append(e.getKey());
	// logInfo.append("=");
	// logInfo.append(e.getValue());
	// logInfo.append(",");
	// }
	// return logInfo.toString();
	// }

//	private boolean isMap(Object parameterObject) {
//		if (parameterObject == null)
//			return false;
//		try {
//			Map tempMap = (Map) parameterObject;
//			return true;
//		} catch (Exception e) {
//			return false;
//		}
//	}

}
